SysManager.UserGroup.UnitTree = function() {

	var currentUnit = new Ext.data.Node();

	var unitTreePanel = new Ext.tree.TreePanel({
				border : false,
				rootVisible : false,
				root : new Ext.tree.TreeNode({
							text : '单位树',
							expanded : true,
							draggable : false
						}),
				loadCustomData : function(menuData) {
					this.root.removeAll();
					for (var i = 0; i < menuData.length; i++) {
						var obj = menuData[i];
						var path = obj.unitCode.toString();
						var parentNode = null;
						var node = new Ext.tree.TreeNode({
									id : obj.id,
									text : obj.unitName,
									code : path,
									draggable : false
								});

						parentNode = this.root;
						if (!Ext.isEmpty(path)) {
							var parentCodeArray = [];
							parentCodeArray = mysplit(path, '.');
							var myparent = null;
							for (var a = 0; a < parentCodeArray.length; a++) {
								myparent = parentNode.findChild('code',
										parentCodeArray[a]);
								if (myparent != null) {
									parentNode = myparent;
								}
							}
						}
						parentNode.appendChild(node);
					}
				},
				listeners : {
					click : clickUnit
				}
			});

	

	function clickUnit(node, e) {
		// 得到当前显示的面板，并且将选中的单位信息传入，触发其加载相应数据.
		var currentId = unitTab.getActiveTab().getId();
		currentUnit = node;
		var unitId = node.attributes.id;
		if (unitId != null && unitId != '') {
			if (currentId == 'accountTab') {
				// 加载用户grid数据.
				accountStore.load({
							params : {
								'unitId' : unitId
							}
						});
			} else if (currentId == 'classTab') {
				classhoursds.load({
							params : {
								start : 0,
								limit : 10,
								propertyId : unitId
							}
						});
			} else if (currentId == 'unitTab') {
				loadUnitForm();
			}
		}//
	}

	return {
		initialize : function() {
			var unitData;
			Ext.Ajax.request({
				url : 'sysunits.action?method=selectAll',
				method : 'GET',
				success : function(response, options) {
					var result = Ext.util.JSON
							.decode(response.responseText);
					if (result.success) {
						unitData = result.data;
						unitTreePanel.loadCustomData(unitData);
						unitTreePanel.expandAll();
					} else {
						Ext.Msg.alert('提示', '不能获得单位信息.');
					}
				},
				failure : function(response, options) {
					Ext.Msg.alert('提示', '不能获得单位信息.');
				}
			});
		},
		
		getTreePanel : function(){
			return unitTreePanel;
		}
	};
	
};
